package cn.tangshh.universal.core.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.configuration.SpringDocConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

/**
 * SpringDoc Config
 * SpringDoc API文档配置
 *
 * @author Tang
 * @version v1.0
 */
@Data
@Slf4j
@Validated
@Configuration
@ConfigurationProperties(prefix = "springdoc")
@ConditionalOnClass(SpringDocConfiguration.class)
public class SpringDocConfig {
    /** API docs title */
    private String title = "API Development Docs";

    /** API version */
    private String version = "1.0";

    /** authorization request header */
    @Pattern(regexp = "[A-Za-z-_ ]+", message = "请求头格式不正确")
    private String authHeader = "Authorization";

    @Bean
    public OpenAPI openApi() {
        return new OpenAPI()
                // Describe info
                .info(new Info().title(title).version(version))
                // Authentication component
                .schemaRequirement(authHeader,
                        new SecurityScheme().name(authHeader).scheme("Bearer").type(SecurityScheme.Type.HTTP)
                )
                .addSecurityItem(new SecurityRequirement().addList(authHeader));
    }
}
